package com.suse.springbook.controller;

import com.suse.springbook.constant.Constants;
import com.suse.springbook.model.BookInfo;
import com.suse.springbook.model.Result;
import com.suse.springbook.model.UserInfo;
import com.suse.springbook.service.BookService;
import com.suse.springbook.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = "/login",produces = "application/json")
    public Result login(String userName, String password, HttpSession session) {
        log.info("用户登录 userName:{},  password:{}",userName , password);
        //校验是否为空
        if(userName.equals("") || password.equals("")) {
            return Result.fail("账户或密码不能为空！");
        }

        //1.根据用户名查找用户信息,判断密码是否正确
        UserInfo  userInfo = userService.queryUserByName(userName);
        if(userInfo == null || userInfo.getId() < 0) {
            return Result.fail("用户不存在！");
        }
        if(!password.equals(userInfo.getPassword())) {
            return Result.fail("账户或密码错误！");
        }
//        //未加入数据库
//        //校验账户或密码是否正确
//        if(!"zhangsan".equals(userName) || !"123456".equals(password)) {
//            return "账户或密码错误！";
//        }
        //存储session
        userInfo.setPassword("");
        session.setAttribute(Constants.USER_SESSION_KEY, userInfo);
        return Result.success("登录成功！");

    }


}
